var pageSize = 20;
var pageStart = 1;
$(function(){
	loadRecommend();
	loadIndex();
	//推荐按钮事件
	$("#recommend").on('click','.del',function(){
		var id = $(this).attr("name");
		delRecommendArticle(id);
	});
	//推荐到首页按钮事件
	$("#index").on('click','.del',function(){
		var id = $(this).attr("name");
		delIndexArticle(id);
	});
});

function delIndexArticle(id){
	$.ajax({
		url:BASE_PATH+'/article/showOnIndex.do',
		type:'post',
		dataType:'json',
		data:{
			id:id,
			showOnIndex:2
		},
		success:function(data){
			if(data.status=='success'){
				$("#index a[name='"+id+"']").parents('tr').remove();
			}else{
				if(data.errorMsg=='expire'){
					window.location.href="login.html"
				}else{
					alert(data.erroeMsg);
				}
			}
		}
	});
}

function delRecommendArticle(id){
	$.ajax({
		url:BASE_PATH+'/article/recommend.do',
		type:'post',
		dataType:'json',
		data:{
			id:id,
			recommend:2
		},
		success:function(data){
			if(data.status=='success'){
				$("#recommend a[name='"+id+"']").parents('tr').remove();
			}else{
				if(data.errorMsg=='expire'){
					window.location.href="login.html"
				}else{
					alert(data.erroeMsg);
				}
			}
		}
	});
}

function loadRecommend(){
	$.ajax({
		url:BASE_PATH+'/article/list.do',
		type:'get',
		dataType : 'json',
		data:{
			pageSize:pageSize,
			pageStart:pageStart,
			recommend:1
		},
		success : function(data) {
			if(data.errorMsg == 'expire'){
				window.location.href="login.html";
			}else{
				createRecommend(data);
			}
		}
	});
}

function loadIndex(){
	$.ajax({
		url:BASE_PATH+'/article/list.do',
		type:'get',
		dataType : 'json',
		data:{
			pageSize:pageSize,
			pageStart:pageStart,
			showOnIndex:1
		},
		success : function(data) {
			if(data.errorMsg == 'expire'){
				window.location.href="login.html";
			}else{
				createIndex(data);
			}
		}
	});
}

function createRecommend(data){
	var html = "";
	if(data.article){
		for(var i = 0; i < data.article.length; i++){
			html+="<tr>"
				+	"<td>"+data.article[i].title+"</td>"
				+	"<td>"+data.navbar[data.article[i].navbar].title+"</td>"
				+	"<td>"+new Date(data.article[i].updateTime).pattern("yyyy-MM-dd")+"</td>"
				+	"<td>"+getTagTd(data.article[i].tag,data.tag)+"</td>"
				+	"<td>"+(data.article[i].draft==1?"草稿":"已发布")+"</td>"
				+"<td>"
				 +"<div class=\"action-buttons\">"
				+		"<a title='取消推荐' class=\"red del\" href=\"#\" name=\""+data.article[i].id+"\">"
				+			"<i class=\"icon-trash bigger-130\"></i>"
				+		"</a>"
				+"</div>"
				 +"</td>"
				+"</tr>";
		}
	}
	$("#recommend").html(html);
}

function createIndex(data){
	var html = "";
	if(data.article){
		for(var i = 0; i < data.article.length; i++){
			html+="<tr>"
				+	"<td>"+data.article[i].title+"</td>"
				+	"<td>"+data.navbar[data.article[i].navbar].title+"</td>"
				+	"<td>"+new Date(data.article[i].updateTime).pattern("yyyy-MM-dd")+"</td>"
				+	"<td>"+getTagTd(data.article[i].tag,data.tag)+"</td>"
				+	"<td>"+(data.article[i].draft==1?"草稿":"已发布")+"</td>"
				+"<td>"
				 +"<div class=\"action-buttons\">"
				+		"<a title='取消首页展示' class=\"red del\" href=\"#\" name=\""+data.article[i].id+"\">"
				+			"<i class=\"icon-trash bigger-130\"></i>"
				+		"</a>"
				+"</div>"
				 +"</td>"
				+"</tr>";
		}
	}
	$("#index").html(html);
}

function getTagTd(tagstr,tagmap){
	var html = "";
	var array = tagstr.split(",");
	for(var i=0;i<array.length;i++){
		if(tagmap[array[i]]){
			html+="<span class='label' style='background-color:"+tagmap[array[i]].tagColor+"!important;margin-right:2px;'>"+tagmap[array[i]].tagName+"</span>";
		}
	}
	return html;
}